package com.college.controller.products;


import com.college.common.pojo.ao.Order;
import com.college.common.pojo.dto.products.AddOrderDTO;
import com.college.common.pojo.dto.products.QueryOrderDTO;
import com.college.common.pojo.dto.products.UpdateOrderDTO;
import com.college.common.pojo.model.page.PageParamRequest;
import com.college.common.response.CommonPage;
import com.college.common.response.CommonResult;
import com.college.common.utils.object.ObjectUtil;
import com.college.products.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单 前端控制器
 * @author 小C工作室
 * @since 2025-02-22
 */
@RestController
@RequestMapping("/order")
@Api(tags = "订单")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    @ApiOperation(value = "查询订单列表")
    @GetMapping(value = "/query-list")
    public CommonResult<List<Order>> queryList(QueryOrderDTO queryOrderDTO) {
        return CommonResult.success(orderService.queryList(queryOrderDTO));
    }

    @ApiOperation(value = "查询订单列表分页")
    @GetMapping(value = "/query-list-page")
    public CommonResult<CommonPage<Order>> queryListPage(QueryOrderDTO queryOrderDTO , PageParamRequest pageParamRequest) {
        return CommonResult.success(CommonPage.restPage(orderService.queryListPage(queryOrderDTO,pageParamRequest)));
    }

    @ApiOperation(value = "新增订单")
    @PostMapping(value = "/add")
    public CommonResult<String> addOrder(@RequestBody AddOrderDTO addOrderDTO) {
        return orderService.addOrder(addOrderDTO);
    }

    @ApiOperation(value = "删除订单")
    @DeleteMapping(value = "/delete/{id}")
    public CommonResult<Boolean> deleteOrder(@PathVariable Long id) {
        return CommonResult.success(orderService.removeById(id));
    }

    @ApiOperation(value = "批量删除订单")
    @DeleteMapping(value = "/batch-delete")
    public CommonResult<Boolean> deleteOrder(@RequestBody List<Long> ids) {
        if (ObjectUtil.isNotEmpty(ids)) {
            return CommonResult.success(orderService.removeByIds(ids));
        }
        return CommonResult.success("参数错误");
    }

    @ApiOperation(value = "更新订单")
    @PutMapping(value = "/update")
    public CommonResult<String> updateOrder(@RequestBody UpdateOrderDTO updateOrderDTO) {
        return orderService.updateOrder(updateOrderDTO);
    }
}

